package ar.com.personalTravellers.rowProcessors;

import ar.com.personalTravellers.beans.*;
import ar.com.personalTravellers.daos.factory.PTDAOFactory;
import com.angel.base.helpers.StringHelper;
import com.angel.io.exceptions.InvalidRowDataException;
import com.angel.io.parameters.ParametersService;
import com.angel.io.processors.imports.ImportRowProcessorCommand;
import com.angel.io.type.rows.impl.HeaderRowFile;
import com.angel.io.type.rows.interfaces.ImportRowFile;

/**
 *
 * @author William
 */
public class HotelRowProcessorCommand implements ImportRowProcessorCommand {

	public static final String URL_ADDRESS_COLUMN = "urlAddress";
    public static final String NAME_COLUMN = "name";
    public static final String ADDRESS_COLUMN = "address";
    public static final String ZIP_CODE_COLUMN = "zipCode";
    public static final String TELEPHONE_COLUMN = "telephone";
    public static final String EMAIL_COLUMN = "email";
    public static final String NEIGHBORHOOD_NAME_COLUMN = "neighborhoodName";
    public static final String TYPE_NAME_COLUMN = "hotelType";

    public static final String PT_DAO_FACTORY_HOTEL_PARAMETER = "ptDAOFactoryHotel";

    public void checkRowData(ImportRowFile importRowFile, HeaderRowFile headerRowFile) throws InvalidRowDataException {
        String name = importRowFile.getStringValue(headerRowFile.getColumnPosition(NAME_COLUMN));
        String urlAddress = importRowFile.getStringValue(headerRowFile.getColumnPosition(URL_ADDRESS_COLUMN));
        String address = importRowFile.getStringValue(headerRowFile.getColumnPosition(ADDRESS_COLUMN));
        String zipCode = importRowFile.getStringValue(headerRowFile.getColumnPosition(ZIP_CODE_COLUMN));
        String telephone = importRowFile.getStringValue(headerRowFile.getColumnPosition(TELEPHONE_COLUMN));
        String email = importRowFile.getStringValue(headerRowFile.getColumnPosition(EMAIL_COLUMN));
        String neighborhoodName = importRowFile.getStringValue(headerRowFile.getColumnPosition(NEIGHBORHOOD_NAME_COLUMN));
        String hotelTypeName = importRowFile.getStringValue(headerRowFile.getColumnPosition(TYPE_NAME_COLUMN));

        boolean allAreNotEmpty = StringHelper.areAllNotEmpty(name, urlAddress, address, zipCode, telephone.toString(), email, neighborhoodName, hotelTypeName);

        if(!allAreNotEmpty || HotelType.valueOf(hotelTypeName.toUpperCase()) == null){
			throw new InvalidRowDataException("Some row data are NULL - " +
					"Name: [" + name + "] - " +
                    "UrlAddress: [" + urlAddress + "] - " +
                    "zipCode: [" + zipCode + "] - " +
                    "telephone: [" + telephone + "] - " +
                    "email: [" + email + "] - " +
                    "neighborhoodName: [" + neighborhoodName + "]."
                    );
		}
    }

    public Object processRow(ImportRowFile importRowFile, HeaderRowFile headerRowFile, ParametersService parametersService) {
        String name = importRowFile.getStringValue(headerRowFile.getColumnPosition(NAME_COLUMN));
        String urlAddress = importRowFile.getStringValue(headerRowFile.getColumnPosition(URL_ADDRESS_COLUMN));
        String email = importRowFile.getStringValue(headerRowFile.getColumnPosition(EMAIL_COLUMN));
        String addressName = importRowFile.getStringValue(headerRowFile.getColumnPosition(ADDRESS_COLUMN));
        String zipCode = importRowFile.getStringValue(headerRowFile.getColumnPosition(ZIP_CODE_COLUMN));
        String telephone = importRowFile.getStringValue(headerRowFile.getColumnPosition(TELEPHONE_COLUMN));
        String neighborhoodName = importRowFile.getStringValue(headerRowFile.getColumnPosition(NEIGHBORHOOD_NAME_COLUMN));
        String hotelTypeName = importRowFile.getStringValue(headerRowFile.getColumnPosition(TYPE_NAME_COLUMN));

        PTDAOFactory ptDAOFactory = (PTDAOFactory) parametersService.findParameter(PT_DAO_FACTORY_HOTEL_PARAMETER);

        Neighborhood neighborhood = ptDAOFactory.getNeighborhoodDAO().findUniqueByName(neighborhoodName);
        HotelType hotelType = HotelType.valueOf(hotelTypeName.toUpperCase()); 

        Address address = new Address();
        address.setZipCode(zipCode);
        address.setStreet(addressName);
        address.setNeighborhood(neighborhood);

        Hotel hotel = new Hotel();
        hotel.addImage(new HotelImage("images/hotel1Externo.jpg", "Imagen Externo del hotel", HotelFileType.EXTERN));
        hotel.addImage(new HotelImage("images/hotel1Interno.jpg", "Imagen Interno del hotel", HotelFileType.INTERN));
        hotel.addImage(new HotelImage("images/hotel1Room.jpg", "Imagen de la mejor habitacion del hotel", HotelFileType.ROOM));
        hotel.setName(name);
        hotel.setUrlAddress(urlAddress);
        hotel.setEmail(email);
        hotel.setTelephone(telephone);
        hotel.setAddress(address);
        hotel.setType(hotelType);

        return hotel;
    }



    public String[] getHeaderColumnsNames() {
        return new String[]{
                NAME_COLUMN,
                URL_ADDRESS_COLUMN,
                EMAIL_COLUMN,
                ADDRESS_COLUMN,
                ZIP_CODE_COLUMN,
                TELEPHONE_COLUMN
        };
    }
}